package cn.donzell.app.utils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import cn.donzell.app.model.Identity;

public class ExcelUtils {

	public static void createExcel(List<Identity> datas, String filePath) {
		HSSFWorkbook workBook = new HSSFWorkbook();
		HSSFSheet sheet = workBook.createSheet();
		HSSFRow headerRow = sheet.createRow(0);
		// 创建单元格，并设置值表头 设置表头居中
		HSSFCellStyle styleMain = workBook.createCellStyle();
		// 水平居中
		styleMain.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		// 垂直居中
		styleMain.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
		sheet.setColumnWidth(0, 10000);
		sheet.setColumnWidth(1, 10000);
		sheet.setColumnWidth(2, 10000);
		sheet.setColumnWidth(3, 10000);
		sheet.setColumnWidth(4, 10000);
		// 为每一行创建两个格子
		HSSFCell headerCel1 = headerRow.createCell(0);
		headerCel1.setCellStyle(styleMain);
		HSSFCell headerCel2 = headerRow.createCell(1);
		headerCel2.setCellStyle(styleMain);
		HSSFCell headerCel3 = headerRow.createCell(2);
		headerCel3.setCellStyle(styleMain);
		HSSFCell headerCel4 = headerRow.createCell(3);
		headerCel4.setCellStyle(styleMain);
		HSSFCell headerCel5 = headerRow.createCell(4);
		headerCel5.setCellStyle(styleMain);
		// 填充每个行中的格子内容
		headerCel1.setCellValue("姓名");
		headerCel2.setCellValue("身份证号");
		headerCel3.setCellValue("地区");
		headerCel4.setCellValue("出生日期");
		headerCel5.setCellValue("性别");
		for (int rowId = 1, index = 0; rowId <= datas.size(); rowId++, index++) {
			Identity identity = datas.get(index);
			HSSFRow dataRow = sheet.createRow(rowId);
			HSSFCell dataCell = dataRow.createCell(0);
			dataCell.setCellValue(identity.getName());
			HSSFCell dataCel2 = dataRow.createCell(1);
			dataCel2.setCellType(HSSFCell.CELL_TYPE_STRING);
			dataCel2.setCellValue(identity.getIdCard());
			HSSFCell dataCel3 = dataRow.createCell(2);
			dataCel3.setCellValue(identity.getArea());
			HSSFCell dataCel4 = dataRow.createCell(3);
			dataCel4.setCellType(HSSFCell.CELL_TYPE_STRING);
			dataCel4.setCellValue(identity.getBirthday());
			HSSFCell dataCel5 = dataRow.createCell(4);
			dataCel5.setCellValue(identity.getSex());
		}
		try {
			FileOutputStream fos = new FileOutputStream(filePath);
			workBook.write(fos);
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readData(String filePath) {
		List<String> datas = new ArrayList<String>();
		try {
			InputStreamReader reader = new InputStreamReader(
					new FileInputStream(System.getProperty("user.dir") + "/config/" + filePath), "utf-8");
			BufferedReader br = new BufferedReader(reader);
			String line = null;
			while ((line = br.readLine()) != null) {
				datas.add(line);
			}
			br.close();
			reader.close();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(new JFrame(),
					"读取文件 " + System.getProperty("user.dir") + "/config/" + filePath + " 出错!");
			return datas;
		}
		return datas;
	}

}
